﻿using System;
using System.Text;
using System.Web;
using Clipping.Infrastructure.Interfaces;
using System.Text.RegularExpressions;

namespace Clipping.SearchUrlConstructors
{
	public class SogouNewsUrlConstructor : ISearchUrlConstruct
	{
		private Encoding _encoding = Encoding.GetEncoding("gb2312");
		public Uri ConstructUrl(Infrastructure.Models.SearchObject searchObj)
		{
			//Contract.Requires(searchObj != null);
			if (searchObj.SearchType == Infrastructure.Models.SearchType.SearchInTitle)
			{
				return new Uri(string.Format("http://news.sogou.com/news?query={0}&mode=2", HttpUtility.UrlEncode(searchObj.Keyword, this._encoding)));
			}
			else
			{
				return new Uri(string.Format("http://news.sogou.com/news?query={0}&mode=1", HttpUtility.UrlEncode(searchObj.Keyword, this._encoding)));

			}
		}

		public Uri ConstructAbsoluteUrl(Uri baseUri, string url)
		{
			if (string.IsNullOrEmpty(url))
			{
				return null;
			}
			else if(url.ToLower().StartsWith("?query"))
			{
			
				string urlStr= "http://news.sogou.com/news" + url;
				return new Uri(urlStr);
			}
			else
				return UriHelper.ConstructAbsoluteUrl(baseUri, url);
		}

	
	}
}
